package org.ekstrabilet.main.gui;

import java.awt.CardLayout;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

import org.ekstrabilet.main.gui.panes.*;

/**
 * Main application window.
 */
public class MainWindow extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 39L;
	
	//Applications state - responsible for switching application states.
	private int userState;
	
	//Window width & height
	private int width;
	private int height;
	
	//GUI components
	private JMenuBar menuBar;
	private JPanel pane;
	
	private String login;
	
	private InitPane initPane;
	private AdminPane adminPane;
	private GuestPane guestPane;
	private SalesPane salesPane;
	private TechPane techPane;
	
	/**
	 * Constructs main window.
	 */
	public MainWindow() {
		super("Ekstrabilet");
	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    
		userState = UserStates.LOGIN;
		login = "";
		
		menuBar = new MainMenu(this).getJMenuBar();
		setJMenuBar(menuBar);
		
		init();
		
	    width = 1024;
	    height = 750;
	    
	    setSize(width, height);
	    setLocation(100, 15);
	    setVisible(true);
	}
	
	private void init() {
		
		initPane = new InitPane(this);
		adminPane = new AdminPane();
		guestPane = new GuestPane();
		salesPane = new SalesPane();
		techPane = new TechPane();
		
		pane = new JPanel(new CardLayout());
		pane.add(initPane.getPane(), Panes.LOGIN);
		pane.add(adminPane.getPane(), Panes.ADMIN);
		pane.add(guestPane.getPane(), Panes.GUEST);
		pane.add(salesPane.getPane(), Panes.SALES);
		pane.add(techPane.getPane(), Panes.TECH);
		
		add(pane);
		
		switchPane();
	}
	
	private void switchPane() {
		
		CardLayout cl = (CardLayout) pane.getLayout();
		switch(userState) {
		case UserStates.LOGIN:
			cl.show(pane, Panes.LOGIN);
			break;
		case UserStates.ADMIN:
			cl.show(pane, Panes.ADMIN);
			break;
		case UserStates.GUEST:
			cl.show(pane, Panes.GUEST);
			break;
		case UserStates.SALES:
			cl.show(pane, Panes.SALES);
			break;
		case UserStates.TECH:
			cl.show(pane, Panes.TECH);
			break;
		}
	}
	
	/**
	 * Sets user's state and updates main pane.
	 * @param state one of states from UserStates interface
	 * @see UserStates
	 */
	public void setUserState(int state) {
		userState = state;
		switchPane();
	}
	
	public int getUserState() {
		return userState;
	}
	
	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
		adminPane.setLogin(login);
		guestPane.setLogin(login);
		techPane.setLogin(login);
		salesPane.setLogin(login);
	}

	public static void main(String[] args) {
		MainWindow w = new MainWindow();
		//w.setUserState(UserStates.ADMIN);
	}
}
